package xkp.lesson5.file;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class JunitTestNio {
    //330ms  315  413
    @Test
    public void testNioCopy() throws IOException {
        copyFile("D:\\eclipse-jee-2019-06-R-win32-x86_64.zip",
                "D:\\250");
    }

    @Test
    public void testNioCopy2() throws IOException {
        FileUtils.copyFileByBuf("D:\\eclipse-jee-2019-06-R-win32-x86_64.zip",
                "D:\\250");
    }

    public  void copyFile(String filePath,String destDir) throws IOException {
        File file = new File(filePath);
        FileChannel srcChannel = new FileInputStream(file).getChannel();
        FileChannel descChanel = new FileOutputStream(
                destDir+File.separator+file.getName()).getChannel();


        //1、transferFrom 从谁里面拷贝到我里面
        descChanel.transferFrom(srcChannel,0,srcChannel.size());
        //2、transferTo  把我里面的数据拷贝到谁里面
        //srcChannel.transferTo(0,srcChannel.size(),descChanel);
        //descChanel.force(true);
        descChanel.close();
        srcChannel.close();
    }
}
